Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
88 lines · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
 * Admin Users API - Update user (ban, promote to admin)
 */
import { getDatabase, schema } from "@/db";
import { users } from "@/db/schema";
import { badRequest, forbidden, notFound, success } from "@/lib/api";
import { withErrorHandler } from "@/lib/errors";
import { logger } from "@/lib/logger";
import { type APIRoute } from "astro";
import { eq } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";

export const PATCH: APIRoute = withErrorHandler(
  async ({ params, request, locals }) => {
    const currentUser = locals.user;

    // Only admins can access
    if (!currentUser?.isAdmin) {
      return forbidden("Admin access required");
    }

    const { userId } = params;
    if (!userId) return badRequest("User ID required");

    const db = getDatabase() as NodePgDatabase<typeof schema>;

    // Find user
    const targetUser = await db.query.users.findFirst({
      where: eq(users.id, userId),
    });
    if (!targetUser) return notFound("User not found");

    // Cannot modify yourself
    if (targetUser.id === currentUser.id) {
      return badRequest("Cannot modify your own account");
    }

    // Parse body
    const body = await request.json();
    const { isAdmin, isActive } = body;

    const updates: any = { updatedAt: new Date() };
    if (typeof isAdmin === "boolean") updates.isAdmin = isAdmin;
    if (typeof isActive === "boolean") updates.isActive = isActive;

    await db.update(users).set(updates).where(eq(users.id, userId));

    // Audit log for admin actions
    const { logAudit, getRequestMeta } = await import("@/lib/audit");
    const { ip, userAgent } = getRequestMeta(request);
    if (typeof isAdmin === "boolean") {
      await logAudit({
        userId: currentUser.id,
        action: isAdmin ? "user.promote_admin" : "user.demote_admin",
        actorIp: ip,
        actorUserAgent: userAgent,
        targetType: "user",
        targetId: userId,
        targetName: targetUser.username,
        data: { before: targetUser.isAdmin, after: isAdmin },
      });
    }
    if (typeof isActive === "boolean") {
      await logAudit({
        userId: currentUser.id,
        action: isActive ? "user.activate" : "user.deactivate",
        actorIp: ip,
        actorUserAgent: userAgent,
        targetType: "user",
        targetId: userId,
        targetName: targetUser.username,
        data: { before: targetUser.isActive, after: isActive },
      });
    }

    logger.info(
      {
        adminId: currentUser.id,
        targetUserId: userId,
        updates: { isAdmin, isActive },
      },
      "User updated by admin",
    );

    return success({ message: "User updated" });
  },
);